#输入变量空值检查,都要检查
chkVars=ciOutDir imageUrl ciContext
$(foreach var,${chkVars},$(eval $(call var_notnull,${var})))

#私有变量定义
myRoot=${projectRoot}
myDir=${moduleDir}
mySources=$(if ${ciSources},${ciSources},${moduleDir}/cmd/main.go)
myTarget=${ciOutDir}/${module}/$(if ${ciBinName},${ciBinName},${module})
myImgUrl=${imageUrl}
myContext=${ciContext}

#空值变量处理
GOPATH?=${HOMER}/go
GOPROXY?=goproxy.io

#宏定义 start
define go_run_direct
export GOPATH=${HOMER}/go
go build -ldflags '-w -s' -o ${myTarget} $(1)
endef

define go_run_docker
docker run --rm -i --name golang_builder -v ${myRoot}:${myRoot} \
  -w ${myRoot} \
  -e "https_proxy=${hp}" \
  -v ${GOPATH}:${GOPATH} \
  -e "GOPATH=${GOPATH}" \
  -e "GOPROXY=${GOPROXY}" \
  --entrypoint=/bin/sh \
  golang:alpine \
  -c "CGO_ENABLED=0 go build -ldflags '-w -s' -o ${myTarget} $(1)"
endef

define goBuild =
$(call go_run_$(T),$(1))
endef
# end宏定义

${GOPATH}/bin/go-bindata:
	go install github.com/jteeuwen/go-bindata/go-bindata

${GOPATH}/bin/go-bindata-assetfs:
	go install github.com/elazarl/go-bindata-assetfs/go-bindata-assetfs

${GOPATH}/bin/protoc-gen-gogofast:
	go install github.com/gogo/protobuf/protoc-gen-gogofast

.PHONY: depx
depx: ${GOPATH}/bin/go-bindata ${GOPATH}/bin/go-bindata-assetfs ${GOPATH}/bin/protoc-gen-gogofast #依赖处理
	@echo "depends install success"

.PHONY: buildx
buildx: #在alpine容器中构建
	$(call goBuild,${mySources})

.PHONY: imagex
imagex: entryx#基于构建二进制，构建镜像
	#@date > files/changer
	$(call func_build_image)

.PHONY: pushx
pushx: #推送镜像到仓库
	@$(call func_push_image)

genx: #生成bfs文件
	@export PATH="${GOPATH}/bin:$${PATH}" &&\
	cd ${moduleDir} && \
	go-bindata-assetfs -pkg bfs -o pkg/bfs/bindata.go --prefix pkg/res -ignore data pkg/res/...
	#sed -i 's|\"strings\"|\"strings\"\n\t\"os\"|g' pkg/bfs/bindata.go

.PHONY: testx
testx: #测试镜像
	docker run --rm -it  --net host ${myImgUrl}

.PHONY: cleanx
cleanx: #清空构建
	rm -f  ${outTarget} && docker rmi -f ${myImgUrl}